function A = merge_struct(A, B, varargin)
    p = inputParser;
    addParameter(p, 'replace', 1);
    parse(p, varargin{:});
    replace = p.Results.replace;

    if replace
        A = rmfield(A, intersect(fieldnames(A), fieldnames(B)));
    else
        B = rmfield(B, intersect(fieldnames(A), fieldnames(B)));
    end
    names = [fieldnames(A); fieldnames(B)];

    A = cell2struct([struct2cell(A); struct2cell(B)], names, 1);
end
